package com.wingice.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wingice.common.page.EntityPageBean;
import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.spring.security.model.KnifeUser;
import com.wingice.system.entity.SysUser;
import com.wingice.system.exception.ResultBean;
import com.wingice.system.service.SysUserService;
import com.wingice.system.validator.sys.user.SysUserValidator;
import com.wingice.system.vo.SysUserView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author 胡昊
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/sysUser")
@Tag(name = "用户管理")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * @param sysUser 用户信息
     * @description 新增用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/24 13:28
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysUser:insert')")
    @PostMapping("/insert")
    @Operation(summary = "新增用户")
    public ResultBean<Boolean> insert(@RequestBody SysUser sysUser) {
        //参数校验
        SysUserValidator.insertValidate(sysUser);
        return ResultBean.restResult(sysUserService.insert(sysUser), ErrorCodeInfo.CREATED);
    }

    /**
     * @param idList 待删除用户id列表
     * @description 批量删除用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/24 13:51
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysUser:deleteBatch')")
    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除用户")
    public ResultBean<Boolean> deleteBatch(@RequestBody List<String> idList) {
        //参数校验
        SysUserValidator.deleteValidate(idList);
        return ResultBean.restResult(sysUserService.deleteBatch(idList), ErrorCodeInfo.NO_CONTENT);
    }

    /**
     * @param sysUser 用户信息
     * @description 更新用户信息
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/24 15:25
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysUser:update')")
    @PatchMapping("/update")
    @Operation(summary = "更新用户")
    public ResultBean<Boolean> update(@RequestBody SysUser sysUser) {
        //参数校验
        SysUserValidator.updateValidate(sysUser);
        return ResultBean.restResult(sysUserService.update(sysUser), ErrorCodeInfo.OK);
    }

    /**
     * @param entityPageBean 查询条件
     * @description 分页查询用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/24 15:39
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysUser:page')")
    @PostMapping("/page")
    @Operation(summary = "分页查询用户")
    public ResultBean<Page<SysUserView>> page(@RequestBody EntityPageBean<SysUser> entityPageBean) {
        return ResultBean.restResult(sysUserService.pageUser(entityPageBean), ErrorCodeInfo.OK);
    }

    /**
     * @description 获取当前登录用户信息
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/9/14 9:56
     */
    @PreAuthorize("@knifeSecurity.authenticated()")
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取当前登录用户信息")
    public ResultBean<KnifeUser> getUserInfo() {
        final KnifeUser knifeUser = (KnifeUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResultBean.restResult(knifeUser, ErrorCodeInfo.OK);
    }

    /**
     * @param oldPassword 老密码
     * @param newPassword 新密码
     * @description 用户修改密码
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2021/3/23 9:04
     */
    @PreAuthorize("@knifeSecurity.authenticated()")
    @PostMapping("/userUpdatePassword")
    @Operation(summary = "用户修改密码")
    public ResultBean<Boolean> userUpdatePassword(String oldPassword, String newPassword) {
        return ResultBean.restResult(sysUserService.userUpdatePassword(oldPassword, newPassword), ErrorCodeInfo.OK);
    }

    /**
     * @param entityPageBean 查询条件
     * @description 模糊搜索用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2021/5/3 19:13
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysUser:searchSysUser')")
    @PostMapping("/searchSysUser")
    @Operation(summary = "模糊搜索用户")
    public ResultBean<Page<SysUser>> searchSysUser(@RequestBody EntityPageBean<String> entityPageBean) {
        return ResultBean.restResult(sysUserService.page(new Page<>(entityPageBean.getPageObject().getPageNum(), entityPageBean.getPageObject().getPageSize()), Wrappers.<SysUser>lambdaQuery()
                .like(StringUtils.hasText(entityPageBean.getEntity()), SysUser::getUsername, entityPageBean.getEntity())
                .or()
                .like(StringUtils.hasText(entityPageBean.getEntity()), SysUser::getName, entityPageBean.getEntity())
                .or()
                .like(StringUtils.hasText(entityPageBean.getEntity()), SysUser::getMail, entityPageBean.getEntity())
                .or()
                .like(StringUtils.hasText(entityPageBean.getEntity()), SysUser::getPhone, entityPageBean.getEntity())
        ), ErrorCodeInfo.OK);
    }
}

